var DateUtil = require('DateUtil'),
    AUTH = require('AUTH');

module.exports = (function ($) {

    var NOOP = function () {
    };

    var URL_API_PREFIX = '/api';

    //get or post data
    function api_call(method, uri, data, cb_success, cb_error, cb_finally) {
        data = data || {};
        for (var i in data) {
            if (data.hasOwnProperty(i) && typeof data[i] === 'function') {
                delete data[i];
            }
            //avoid empty string problem
            if (data.hasOwnProperty(i) && data[i] === '') {
                delete data[i];
            }
        }
        if (!method) throw new Error("method is null");
        if (!uri) throw new Error("url is null");

        cb_success = (cb_success && typeof cb_success === 'function') ? cb_success : NOOP;
        cb_error = (cb_error && typeof cb_error === 'function') ? cb_error : function (msg, state) {
                alert("" + state + ":" + JSON.stringify(msg));
            };
        cb_finally = (cb_finally && typeof cb_finally === 'function') ? cb_finally : NOOP;

        $.ajax({
            type: method,
            url: URL_API_PREFIX + uri,
            data: data,
            headers: {
                Authorization: AUTH.getToken()
            },
            //dataType: "json",
            success: function (data, status, xhr) {
                cb_finally(cb_success(data, status, xhr));
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var message = XMLHttpRequest.responseText;
                //var messageJsonMessage = XMLHttpRequest.responseJSON.message;
                var state = XMLHttpRequest.status;
                //console.log(XMLHttpRequest);
                //console.log(XMLHttpRequest);
                if (state >= 500) {
                    alert("服务器出错！" + message);
                }
                else if (state === 403) {
                    try {
                        var err = JSON.parse(message);
                        if (err.code == 403001) {
                            window.location.href = "/login?uri=" + encodeURIComponent(window.location.pathname);
                        }
                    }
                    catch (e) {
                        alert("拒绝访问! " + message );
                    }
                }
                else {
                    cb_error(message, state);
                }
                cb_finally();
            }
        });
    };

    var overlay = (function () {
        var html = "<div class='cover'><i class='fa fa-spinner fa-pulse'></i></div>";
        var $overlay = $(html);
        return {
            close: function () {
                $overlay.remove();
            },
            show: function () {
                this.close();
                $('body').prepend($overlay);
            }
        }
    })();

    //DAO --DATA ACCESS OBJECT
    return {


            report: {
            add: function (qo, cb_suc, cb_err) {
                api_call('post', '/report/', qo, cb_suc, cb_err, overlay.close);
            }
        }
    }
})(jQuery);